package com.zhw.andfix;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.alipay.euler.andfix.patch.PatchManager;

import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.NetProvider;
import cn.droidlover.xdroidmvp.net.RequestHandler;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.jpush.android.api.JPushInterface;
import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

/**
 * Created by zhonghw on 2016/3/10.
 */
public class BaseApplication extends Application {

    private static final String TAG = "AndFix";
    public static String VERSION_NAME = "";
    public static PatchManager mPatchManager;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            PackageInfo mPackageInfo = this.getPackageManager().getPackageInfo(
                    this.getPackageName(), 0);
            VERSION_NAME = mPackageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        JPushInterface.init(this);
        initAndFix();
        initProvider();
    }


    private void initAndFix() {
        // initialize

        mPatchManager = new PatchManager(this);
        mPatchManager.init(VERSION_NAME);
        Log.d(TAG, "inited.");

        // load patch
        mPatchManager.loadPatch();
//        Log.d(TAG, "apatch loaded.");
    }

    private void initProvider(){
        XApi.registerProvider(new NetProvider() {

            @Override
            public Interceptor[] configInterceptors() {
                return new Interceptor[0];
            }

            @Override
            public void configHttps(OkHttpClient.Builder builder) {

            }

            @Override
            public CookieJar configCookie() {
                /*return new CookieJar() {
                    //Cookie缓存区
                    MyCookieManager manager = MyCookieManager.newInstance();
                    PersistentCookieStore store = PersistentCookieStore.newInstance();
                    @Override
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        //移除相同的url的Cookie
                        String host = url.host();
                        List<Cookie> cookiesList = manager.getCookies(host);
                        if (cookiesList != null){
                            manager.remove(host);
                        }
                        //再重新天添加
                        manager.add(host, cookies);
                        store.add(host, cookies);
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl url) {
                        if (manager.getCookiesMap().isEmpty()){
                            List<Cookie> cookies = store.getCookies(url.host());
                            manager.add(url.host(), cookies);
                        }
                        List<Cookie> cookiesList = manager.getCookies(url.host());
                        //注：这里不能返回null，否则会报NULLException的错误。
                        //原因：当Request 连接到网络的时候，OkHttp会调用loadForRequest()
                        return cookiesList != null ? cookiesList : new ArrayList<Cookie>();
                    }
                };*/
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 15000;
            }

            @Override
            public long configReadTimeoutMills() {
                return 15000;
            }

            @Override
            public boolean configLogEnable() {
                return true;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }

//            @Override
//            public boolean dispatchProgressEnable() {
//                return false;
//            }
        });
    }
}
